/*
Source : https://leetcode.com/problems/length-of-last-word/
Author : nflush@outlook.com
Date   : 2016-07-04
*/

/*
58. Length of Last Word
Total Accepted: 99659 Total Submissions: 334233 Difficulty: Easy

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

For example,
Given s = "Hello World",
return 5.

Subscribe to see which companies asked this question
Have you met this question in a real interview?
*/

// 4ms
class Solution {
public:
    int lengthOfLastWord(string s) {
        int len =0;
        bool b = false;

        for (int i=0;i<s.size();i++){
            if (s[i] == ' '){
                b = true;
            } else {
                if (b){
                    len = 0;
                    b = false;
                }
                len++;
            }
        }
        return len;
    }
};

// 4ms
class Solution {
public:
    int lengthOfLastWord(string s) {
        int len =0;
        bool b = false;

        for (int i=s.size();i-->0;){
            if (s[i] == ' '){
                if (b) break;
            } else {
                len++;
                b = true;
            }
        }
        return len;
    }
};


// 4ms
class Solution {
public:
    int lengthOfLastWord(string s) {
        int len =0;
        int i;
        for (i=s.size();i--&&s[i] == ' ';);

        for (i++;i-- && s[i] != ' ';len++);
        return len;
    }
};

